package com.jingbo.DAO.User;

import com.jingbo.Dbutil.DBUtil;
import com.jingbo.entity.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.jingbo.DAO.User
 * @Description:
 * @Date 2022/5/27 12:59
 */
public class Adduserimpl implements  AddUser{
    private QueryRunner qr = new QueryRunner(DBUtil.getDateSource());
    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public int AddUserOne(User user) {
        String sql ="insert into t_user(username,pwd,status)values(?,?,?)";
        try {
            int update = qr.update(sql, user.getUsername(), user.getPassword(), user.getStatus());
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 用户是否存在
     * @param username
     * @param pwd
     * @return
     */
    @Override
    public User selectuser(String username, String pwd) {
        String sql ="select * from t_user where username =? and pwd=?  ";
        try {
            return qr.query(sql, new BeanHandler<User>(User.class),username, pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
